﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.Shop
{
    public partial class buyingLeads : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(buyingLeads));
        }
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetNewsCount(string shop)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            if (CommonBll.IsLegitimateShopId(shop))
            {
                var count = NewsInfoBll.GetNewsInfoCount(" NC_Code='001' and News_Status=1 and Client_Id=" + CommonBll.GetTheRealId(shop).ToString(), CommonConst.DB_Name);
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", count);
            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "0");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetNewsList(int pageIndex, int pageSize, string shop)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            if (CommonBll.IsLegitimateShopId(shop))
            {
                try
                {
                    StringBuilder builder = new StringBuilder();
                    var dicWhere = new Dictionary<string, object>();
                    dicWhere.Add("NC_Code", "001");
                    dicWhere.Add("Client_Id", CommonBll.GetTheRealId(shop).ToString());
                    dicWhere.Add("News_Status", 1);
                    var dataTable = NewsInfoBll.GetNewsInfoTable(pageIndex, pageSize, "*", "News_Date desc", dicWhere, CommonConst.DB_Name);
                    foreach (DataRow row in dataTable.Rows)
                    {
                        builder.AppendFormat("<li><span>{0}</span><a href=\"/html/001/{1}.html\" target=\"_blank\">{2}</a></li>", 
                            Convert.ToDateTime(row["News_Date"]).ToString("yyyy/MM/dd"), 
                            EncryptHelper.EncryptString(row["News_Id"].ToString()),
                            row["News_Title"]);
                    }
                    jsonResult.Add("isreload", true);
                    jsonResult.Add("msg", builder.ToString());
                }
                catch (Exception ex)
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }

            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "0");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
